<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2016-2017 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;

/**
 * 渠道来源管理
 *
 * @author Xmh
 */
class Join {

    private $JoinModel;

    public function __construct() {
        $this->JoinModel = M("Join");
    }

    /**
     * 获得加盟列表
     */
    public function get_join_list($page = 1, $size = 10, $params) {
        $map = array();
        /**
         * 条件查询
         * 1、登记ID 2、手机号 3、渠道来源 4、开始时间、结束时间 5、操作人 6、车商帐号
         */
        if ($params['book_id']) {
            $map['book_id'] = array('LIKE', '%' . $params['book_id'] . '%');
        }
        if ($params['phone']) {
            $map['phone'] = array('LIKE', '%' . $params['phone'] . '%');
        }
        if ($params['comefrom']) {
            $map['comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['dealer_name']) {
            $map['dealer_name'] = array('elt', '%' . $params['dealer_name'] . '%');
        }
        if ($params['status']) {
            $map['status'] = array('eq', $params['status']);
        }
        if ($params['start_time']) {
            $map['book_time'] = array('egt', $params['start_time'] . ' 00:00:00');
        }
        if ($params['end_time']) {
            $map['book_time'] = array('elt', $params['end_time'] . ' 23:59:59');
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['book_time'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        }
        $cols = "*";
        $order = "";
        $total = $this->JoinModel->where($map)->count();
        $list = $this->JoinModel->alias('j')
                ->field($cols)
                ->where($map)
                ->limit($size)
                ->page($page)
                ->order($order)
                ->select();
//        echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 提交加盟信息
     * @params array $data;
     * return int
     */
    public function add_join_post($data) {
        $condition['phone'] = $data['phone'];
        $condition['status'] = 1;
        $total = $this->JoinModel->where($condition)->count();
        if ($total > 0) {
            AuctionException::make_throw("200077");
        }
        return $this->JoinModel->add($data);
    }

    /**
     * 更新
     * @params array $data;
     * return int
     */
    public function update_join_post($data) {
        $data['book_time'] = date("Y-m-d H:i:s", time());
        return $this->JoinModel->save($data);
    }

    /**
     * 查看已登记信息
     * @params int $id 登记ID
     * return array
     */
    public function get_join_info($id) {
        $condition['id'] = $id;
        return $this->JoinModel->where($condition)->find();
    }

    /**
     * 获取加盟登记日志
     */
    public function get_join_log() {
        
    }

}
